package cn.mesmile.order.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zb
 * @version 1.0
 * @date 2020/10/25 10:41
 * @description   自定义  熔断和降级 返回信息
 */
@Component
public class MySentinelBlockHandler implements BlockExceptionHandler {

    /*
        FlowException  //限流异常
        DegradeException  //降级异常
        ParamFlowException //参数限流异常
        SystemBlockException //系统负载异常
        AuthorityException //授权异常
     */

    /**
     *  V2.1.0 到  V2.2.0后，sentinel 里面依赖进行改动，且不向下兼容
     * @param request
     * @param response
     * @param e
     * @throws Exception
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        System.out.println("request.getRequestURI() = " + request.getRequestURI());
        System.out.println("request.getRemoteUser() = " + request.getRemoteUser());
        System.out.println("request.getContextPath() = " + request.getContextPath());
        System.out.println("request.getMethod() = " + request.getMethod());

        // 降级业务
        Map<String,Object> backMap=new HashMap<>();
        if (e instanceof FlowException){
            backMap.put("code",-1);
            backMap.put("msg","限流-异常啦");
        }else if (e instanceof DegradeException){
            backMap.put("code",-2);
            backMap.put("msg","降级-异常啦");
        }else if (e instanceof ParamFlowException){
            backMap.put("code",-3);
            backMap.put("msg","热点-异常啦");
        }else if (e instanceof SystemBlockException){
            backMap.put("code",-4);
            backMap.put("msg","系统规则-异常啦");
        }else if (e instanceof AuthorityException){
            backMap.put("code",-5);
            backMap.put("msg","认证-异常啦");
        }

        // 设置返回json数据
        response.setStatus(200);
        response.setHeader("content-Type","application/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(backMap));

    }
}
